package com.open.demo.compose

import android.os.Bundle
import androidx.activity.compose.setContent
import androidx.appcompat.app.AppCompatActivity
import androidx.compose.foundation.*
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyRow
import androidx.compose.foundation.lazy.itemsIndexed
import androidx.compose.material.Text
import androidx.compose.runtime.*
import androidx.compose.ui.ExperimentalComposeUiApi
import androidx.compose.ui.Modifier
import androidx.compose.ui.focus.onFocusChanged
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.open.demo.R

/**
 * 使用Jetpack Compose编写一款简单的AndroidTV应用
 * https://juejin.cn/post/7018880391515734047
 *
 * Focus处理 官方sample
 * https://github.com/androidx/androidx/blob/androidx-main/compose/ui/ui/samples/src/main/java/androidx/compose/ui/samples/FocusSamples.kt
 *
 * SampleComposeAppddd
 * https://github.com/akilarajeshks/SampleComposeApp
 */
class JetpackComposeActivity: AppCompatActivity() {

    companion object {
        const val TAG = "Jetpack Compose Demo"
    }

    @ExperimentalComposeUiApi
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            Greeting(TAG)
        }
    }

    @ExperimentalComposeUiApi
    @Preview
    @Composable
    fun Greeting(name: String) {
        LazyRow(
            modifier = Modifier
                .fillMaxWidth()
                .fillMaxHeight(),
            contentPadding = PaddingValues(60.dp),
            horizontalArrangement = Arrangement.spacedBy(10.dp)
        ) {
            itemsIndexed(listOf(
                Item("榜单", R.drawable.mainview_cloudlist),
                Item("活动", R.drawable.mainview_huodong),
                Item("游戏", R.drawable.mine_handler),
                Item("影视", R.drawable.mine_account)
            )) { index,item ->
                if (index == 3) {
                    Column(verticalArrangement = Arrangement.spacedBy(10.dp)) {
                        var isFocused by remember { mutableStateOf(false) }
                        ItemWidget.ItemView(
                            item = item,
                            isFocused = isFocused,
                            modifier = Modifier
                                .onFocusChanged {
                                    isFocused = it.isFocused
                                }
                                .size(200.dp, 105.dp)
                        )
                        var isFocused2 by remember { mutableStateOf(false) }
                        ItemWidget.ItemView(
                            item = Item("音乐", R.drawable.mainview_tuijian),
                            isFocused = isFocused2,
                            modifier = Modifier
                                .onFocusChanged {
                                    isFocused2 = it.isFocused
                                }
                                .size(200.dp, 105.dp)
                        )
                    }
                } else {
                    var isFocused by remember { mutableStateOf(false) }
                    ItemWidget.ItemView(
                        item = item,
                        isFocused = isFocused,
                        modifier = Modifier
                            .onFocusChanged {
                                isFocused = it.isFocused
                            }
                            .size(146.dp, 220.dp)
                    )
                }
            }
        }
    }

    /**
     * https://developer.android.com/jetpack/compose/graphics
     */
    @Preview
    @Composable
    private fun graphicsCanvas() {
        Canvas(modifier = Modifier.fillMaxSize()) {
            drawRect(
                color = Color.Green
            )
        }
    }

    /**
     * 滚动修饰符 https://developer.android.com/jetpack/compose/gestures#scroll-modifiers
     * 让用户在元素内容边界大于最大尺寸约束时滚动元素
     */
    @Preview
    @Composable
    private fun scrollModifiers() {
        val state = rememberScrollState()
//        state.animateScrollTo(100)
        Column(
            modifier = Modifier
                .background(Color.Red)
                .size(100.dp)
                .padding(horizontal = 8.dp)
                .verticalScroll(state)
        ) {
            repeat(10) {
                Text("Item $it", modifier = Modifier.padding(2.dp))
            }
        }

        Modifier
            .verticalScroll(state)
            .horizontalScroll(rememberScrollState())
    }

}

/*
colorResource()
stringResource()
imageResource()

fun Text(
    text: String, // 文本
    // Modifier 一个 有序的、不可变的修饰元素集合，用于添加装饰或者行为到Compose UI元素。例如background、padding 、点击事件等。
    modifier: Modifier = Modifier,
    color: Color = Color.Unset,  // 文字颜色
    fontSize: TextUnit = TextUnit.Inherit,  // 文字大小
    fontStyle: FontStyle? = null, // 绘制字母时使用的字体变体（例如，斜体）
    fontFamily: FontFamily? = null, // 呈现文本时要使用的字体系列
    letterSpacing: TextUnit = TextUnit.Inherit,  // 字间距
    textDecoration: TextDecoration? = null, // 文字装饰、比如下划线
    textAlign: TextAlign? = null,  // 对齐方式
    lineHeight: TextUnit = TextUnit.Inherit, // 行高
    overflow: TextOverflow = TextOverflow.Clip, // 文字显示不完的处理方式，例如尾部显示…或者中间显示…
    // 文本是否应在换行符处中断。如果为false，则文本的宽度会在水平方向上无限延伸，且textAlign属性失效，可能会出现异常情况。
    softWrap: Boolean = true,
    maxLines: Int = Int.MAX_VALUE, // 最大行数
    // A map store composables that replaces certain ranges of the text. It's
    // used to insert composables into text layout. Check [InlineTextContent] for more information.
    inlineContent: Map<String, InlineTextContent> = mapOf(),
    onTextLayout: (TextLayoutResult) -> Unit = {}, // 计算新的文本布局时执行的回调
    // 文本的样式配置，例如颜色，字体，行高等。也就是说上面属性中的color,fontSize等一些属性也可以在这里进行声明。具体包含的属性可以参考TextStyle类。
    style: TextStyle = currentTextStyle()
)

fun Modifier.graphicsLayer(
    scaleX: Float = 1f,
    scaleY: Float = 1f,
    alpha: Float = 1f,
    translationX: Float = 0f,
    translationY: Float = 0f,
    shadowElevation: Float = 0f,
    rotationX: Float = 0f,
    rotationY: Float = 0f,
    rotationZ: Float = 0f,
    cameraDistance: Float = DefaultCameraDistance,
    transformOrigin: TransformOrigin = TransformOrigin.Center,
    shape: Shape = RectangleShape,
    clip: Boolean = false
)
 */